#pragma once
#include <iostream>
#include "Common.hpp"

class Init
{
public:
    Init()
    {
        ::umask(0);
        // 创建命名管道
        int n = mkfifo(gpipename, mode);
        if (n < 0)
        {
            std::cout << "mkfifo failed..." << std::endl;
            return;
        }
        std::cout << "mkfifo success..." << std::endl;
    }
    ~Init()
    {
        int n = ::unlink(gpipename);
        if (n < 0)
        {
            std::cout << "unlink failed..." << std::endl;
            return;
        }
        std::cout << "unlink success..." << std::endl;
    }
};
Init init;
//服务端 接收数据
class Server
{
public:
    Server():_fd(-1)
    {
    }
    //std::string *  表示输出型参数
    //const std::string & 表示输入型参数
    //std::string& 表示输入输出型参数
    bool OpenPipeForRead()
    {
        _fd=OpenPipe(gOnlyRead);
        if (_fd < 0) return false;
        return true;
    }
    int RecePipe(std::string *out)
    {
        char buffer[gsize];
        int n=::read(_fd,buffer,sizeof(buffer)-1);
        if(n>0)
        {
            buffer[n]=0;
        }
        *out=buffer;
        return n;
    }
    void ClosePipe()
    {
        _close(_fd);
    }
    ~Server()
    {

    }
private:
    int _fd;
};